package cn.com.cfae.iras.doc.analyze.components.iaad;

import cn.com.cfae.iras.doc.analyze.beans.iaad.IaadIndicatorGradeDetail;
import cn.com.cfae.iras.doc.analyze.beans.iaad.IndustryIndicatorList;
import cn.com.cfae.iras.doc.analyze.beans.iaad.IndustryIndicatorThreshold;
import cn.com.cfae.iras.doc.analyze.beans.web.ContentAndPosition;
import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.model.ChapterModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractBizModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import com.google.gson.GsonBuilder;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * Created by 佟媛媛 on 18/11/7.
 * 火电
 */

@Component("HD05")
@Scope("prototype")
public class HD05 extends BaseComponent {

    private static Logger log = LoggerFactory.getLogger(HD05.class);



    public List<IaadIndicatorGradeDetail> handlerIAAD(){
        List<IaadIndicatorGradeDetail> list = new ArrayList<>();
        IaadIndicatorGradeDetail ZTPJ = getZTPJ();//主体评级
        if(ZTPJ!=null) list.add(ZTPJ);
        IaadIndicatorGradeDetail SXZC_ZZC = getSXZC_ZZC();//受限资产/总资产
        if(SXZC_ZZC!=null) list.add(SXZC_ZZC);
        IaadIndicatorGradeDetail XJLL = getXJLL();//经营性现金流量净额
        if(XJLL!=null) list.add(XJLL);
        IaadIndicatorGradeDetail JLR_JZC = getJLR_JZC();//净利润/净资产
        if(JLR_JZC!=null) list.add(JLR_JZC);
        IaadIndicatorGradeDetail MLL = getMLL();//毛利率
        if(MLL!=null) list.add(MLL);
        IaadIndicatorGradeDetail ZZC = getZZC();//总资产规模
        if(ZZC!=null) list.add(ZZC);
        IaadIndicatorGradeDetail ZFZ_ZZC = getZFZ_ZZC();//有效资产负债率
        if(ZFZ_ZZC!=null) list.add(ZFZ_ZZC);
        List<IaadIndicatorGradeDetail> FDXSS = getFDXSS();//发电利用小时数
        if(CollectionUtils.isNotEmpty(FDXSS)) list.addAll(FDXSS);
        log.info("火电行业指标：{}",new GsonBuilder().create().toJson(list));
        return list;
    }

    //    发电利用小时数
    /**
     1、发电利用小时数 --》FDXSS
     "1、设备利用小时数，发行人的数据；
     2、如果获取到多个，人工选择"
     */
    private List<IaadIndicatorGradeDetail> getFDXSS(){
        List<IaadIndicatorGradeDetail> list = new ArrayList<>();
        try{
            int pageNum = getChapterModel("M-5").getStartPage();
            IndustryIndicatorList indicator = getIndustryIndicatorList("FDLYXSS");
            List<KVText> kvTextList = getKVTextValues("M-5","HD","FDXSS");
            if(CollectionUtils.isNotEmpty(kvTextList)){
                for(KVText kvText : kvTextList){
                    String value = kvText.getText();
                    double v = getDouble(value);
                    List<ContentAndPosition> textPositionList = getContentAndPositionList2(kvText);
                    if(StringUtils.isEmpty(value)){
                        continue;
                    }
                    IaadIndicatorGradeDetail iaadIndicatorGradeDetail = getIaadIndicatorGradeDetail(indicator,textPositionList,doubleToBigDecimal(v),pageNum,value,String.format("发电利用小时数%s",value));
                    if(iaadIndicatorGradeDetail!=null) list.add(iaadIndicatorGradeDetail);
                }
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return list;
    }

    //    有效资产负债率
    /**
     2、有效资产负债率 --》负债总额（负债总计、总负债）/资产总额（资产总计、总资产）--》ZFZ/ZZC
     --》M-6、ZCFZ.FZHJ、ZCFZ.ZCZJ
     "1、近一年或近一期取较高的值；
     2、从会计科目分析章节中的偿债能力中获取；
     3、并添加备注：有效资产负债率计算时，需先调整负债端和资产端。其中负债端应增加计入权益的负债，如永续债、明股实债等；资产端应扣减公益性资产；"
     单位：%
     */
    private IaadIndicatorGradeDetail getZFZ_ZZC(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("YXZCFZL");
            //        直接抽取
            String ZCFZLS1 = null,ZCFZLS2 = null;
            double ZCFZLD1 = 0,ZCFZLD2 = 0;
            int pageNum = getChapterModel("M-6").getStartPage();
            ExtractBizModel model = getModel("M-6","CZNL");
            Map<String, ExtractItem> CZNLcol1 = model.getTable(getJYN(model.getTables()));
            if(MapUtils.isNotEmpty(CZNLcol1) && CZNLcol1.get("ZCFZL")!=null && CZNLcol1.get("ZCFZL").getValue()!=null){
                ZCFZLS1 = CZNLcol1.get("ZCFZL").getValue().getText();
                ZCFZLD1 = (getDouble(ZCFZLS1));
                pageNum = CZNLcol1.get("ZCFZL").getValue().getPageNumber();
            }
            Map<String, ExtractItem> CZNLcol2 = model.getTable(getJYQ(model.getTables()));
            if(MapUtils.isNotEmpty(CZNLcol2) && CZNLcol2.get("ZCFZL")!=null && CZNLcol2.get("ZCFZL").getValue()!=null){
                ZCFZLS2 = CZNLcol2.get("ZCFZL").getValue().getText();
                ZCFZLD2 = (getDouble(ZCFZLS2));
                pageNum = CZNLcol2.get("ZCFZL").getValue().getPageNumber();
            }
            if(StringUtils.isEmpty(ZCFZLS1) && StringUtils.isEmpty(ZCFZLS2)){
                return null;
            }
            if(!ZCFZLS1.contains("%")) ZCFZLS1=ZCFZLS1+"%";
            if(!ZCFZLS2.contains("%")) ZCFZLS2=ZCFZLS2+"%";
            return getIaadIndicatorGradeDetail(indicator,(ZCFZLD1>ZCFZLD2?doubleToBigDecimal(ZCFZLD1):doubleToBigDecimal(ZCFZLD2)),"",pageNum,String.format("近一年资产负债率%s，近一期资产负债率%s",ZCFZLS1,ZCFZLS2));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
//        double v1 = 0,v2 = 0,FZHJ1 =0,ZCZJ1 = 0,FZHJ2 =0,ZCZJ2 = 0;
//        String FZHJS1=null,ZCZJS1=null,FZHJS2=null,ZCZJS2=null;
//        Map<String, ExtractItem> col1 = getModel("M-6","ZCFZ").getTable(getJYN());
//        Map<String, ExtractItem> col2 = getModel("M-6","ZCFZ").getTable(getJYQ());
//        if(MapUtils.isNotEmpty(col1)){
//            if(col1.get("FZHJ")!=null && col1.get("FZHJ").getValue()!=null){
//                FZHJS1 = col1.get("FZHJ").getValue().getText();
//                FZHJ1 = getDouble(FZHJS1);
//            }
//            if(col1.get("ZCZJ")!=null && col1.get("ZCZJ").getValue()!=null){
//                ZCZJS1 = col1.get("ZCZJ").getValue().getText();
//                ZCZJ1 = getDouble(ZCZJS1);
//            }
//            if(FZHJ1!=0 && ZCZJ1!=0){
//                v1 = (FZHJ1/ZCZJ1);
//            }
//        }
//        if(MapUtils.isNotEmpty(col2)){
//            if(col2.get("FZHJ")!=null && col2.get("FZHJ").getValue()!=null){
//                FZHJS2 = col2.get("FZHJ").getValue().getText();
//                FZHJ2 = getDouble(FZHJS2);
//            }
//            if(col2.get("ZCZJ")!=null && col2.get("ZCZJ").getValue()!=null){
//                ZCZJS2 = col2.get("ZCZJ").getValue().getText();
//                ZCZJ2 = getDouble(ZCZJS2);
//            }
//            if(ZCZJ2!=0){
//                v2 = (FZHJ2/ZCZJ2);
//            }
//        }
//        double v = v1-v2>0?v1:v2;
//        if(StringUtils.isEmpty(FZHJS1) && StringUtils.isEmpty(ZCZJS1) && StringUtils.isEmpty(FZHJS2) && StringUtils.isEmpty(ZCZJS2)){
//            return null;
//        }
//        return getIaadIndicatorGradeDetail(indicator,v+"",0,String.format("近一年%s，近一期%s",v1,v2));
    }

    //    总资产规模
    /**
     3、总资产规模 --》ZZC
     --》M-6、ZCFZ.ZCZJ
     1、从资产负债表获取（并表），获取近一年或近一期中较低的值；
     */
    private IaadIndicatorGradeDetail getZZC(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("ZZCGM");
            double v1 = 0,v2 = 0,ZCZJ = 0;
            String s1=null,s2=null;
            String unit = "";
            int pageNum = getChapterModel("M-6").getStartPage();
            ExtractBizModel model = getModel("M-6","ZCFZ");
            Map<String, ExtractItem> col1 = model.getTable(getJYN(model.getTables()));
            Map<String, ExtractItem> col2 = model.getTable(getJYQ(model.getTables()));
            if(MapUtils.isNotEmpty(col1) && col1.get("ZCZJ")!=null && col1.get("ZCZJ").getValue()!=null){
                pageNum = col1.get("ZCZJ").getValue().getPageNumber();
                s1 = col1.get("ZCZJ").getValue().getText();
                v1 = getDouble(s1);
            }
            if(MapUtils.isNotEmpty(col2) && col2.get("ZCZJ")!=null && col2.get("ZCZJ").getValue()!=null){
                pageNum = col1.get("ZCZJ").getValue().getPageNumber();
                s2 = col2.get("ZCZJ").getValue().getText();
                v2 = getDouble(s2);
            }
            ZCZJ = v1-v2>0?v2:v1;
            if(StringUtils.isEmpty(s1) && StringUtils.isEmpty(s2)){
                return null;
            }
            unit = model.getUnitType();
            s1 = s1+unit;
            s2 = s2+unit;
            return getIaadIndicatorGradeDetail(indicator,doubleToBigDecimal(ZCZJ),unit,pageNum,String.format("近一年总资产规模%s，近一期总资产规模%s",s1,s2));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

//    毛利率
    /**
     4、毛利率 --》MLL --》（营业收入-营业成本）/营业收入 --》(YYSR-YYCB)/YYSR --》(目前采用抽取值，不适用公式计算)
     --》M-5、MLL.MLL 或 LRM.YYSR、LRM.YYCB
     "1、近一年或近一期取较低的值；
     2、从M-5章节中的主营业务分析中的毛利率表格中获取电力业务板块的毛利率；"
     单位：%
     */
    private IaadIndicatorGradeDetail getMLL(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("MLL");
    //        直接抽取
            String MLLS = null,MLLS2 = null;
            double MLLD = 0,MLLD2 = 0;
            boolean boo = false;
            boolean boo2 = false;
            int pageNum = getChapterModel("M-5").getStartPage();
            ExtractBizModel model = getModel("M-5","MLL");
            List<Map<String, ExtractItem>> mll = model.getTable2(getJYN(model.getTables2()));
            String[] arr = new String[]{"火电","电力","发电"};
            for(int i=0;i<arr.length;i++){
                if(mll==null) break;
                if(boo) break;
                for(Map<String, ExtractItem> item : mll){
                    if(boo) break;
                    if(item.get("YWBK")!=null && item.get("YWBK").getValue()!=null){
                        if(StringUtils.isNotEmpty(item.get("YWBK").getValue().getText()) && item.get("YWBK").getValue().getText().contains(arr[i])){
                            if(item.get("ZB")!=null && item.get("ZB").getValue()!=null){
                                MLLS = item.get("ZB").getValue().getText();
                                MLLD = getDouble(MLLS);
                                pageNum = item.get("ZB").getValue().getPageNumber();
                            }
                            boo = true;
                        }
                    }
                }
            }
            List<Map<String, ExtractItem>> mll2 = model.getTable2(getJYQ(model.getTables2()));
            String[] arr2 = new String[]{"火电","电力","发电"};
            for(int i=0;i<arr2.length;i++){
                if(mll2==null) break;
                if(boo2) break;
                for(Map<String, ExtractItem> item : mll2){
                    if(boo2) break;
                    if(item.get("YWBK")!=null && item.get("YWBK").getValue()!=null){
                        if(StringUtils.isNotEmpty(item.get("YWBK").getValue().getText()) && item.get("YWBK").getValue().getText().contains(arr[i])){
                            if(item.get("ZB")!=null && item.get("ZB").getValue()!=null){
                                MLLS2 = item.get("ZB").getValue().getText();
                                MLLD2 = getDouble(MLLS2);
                                pageNum = item.get("ZB").getValue().getPageNumber();
                            }
                            boo2 = true;
                        }
                    }
                }
            }
            if(StringUtils.isEmpty(MLLS) && StringUtils.isEmpty(MLLS2)){
                return null;
            }
            if(!MLLS.contains("%")) MLLS = MLLS+"%";
            if(!MLLS2.contains("%")) MLLS2 = MLLS2+"%";
            return getIaadIndicatorGradeDetail(indicator,(MLLD>MLLD2?doubleToBigDecimal(MLLD2):doubleToBigDecimal(MLLD)),"",pageNum,String.format("近一年毛利率%s，近一期毛利率%s",MLLS,MLLS2));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
//        使用公式计算（营业收入-营业成本）/营业收入
//        Map<String, ExtractItem> JLRcol = getModel("M-6","LR").getTable(getSNJYQ()[2]);
//        String YYSRS = null,YYCBS = null;
//        double YYSR = 0,YYCB = 0,v = 0;
//        if(MapUtils.isNotEmpty(JLRcol) && JLRcol.get("YYSR")!=null && JLRcol.get("YYSR").getValue()!=null){
//            YYSRS = JLRcol.get("YYSR").getValue().getText();
//            YYSR = getDouble(YYSRS);
//        }
//        if(MapUtils.isNotEmpty(JLRcol) && JLRcol.get("YYCB")!=null && JLRcol.get("YYCB").getValue()!=null){
//            YYCBS = JLRcol.get("YYCB").getValue().getText();
//            YYCB = getDouble(YYCBS);
//        }
//        if(YYSR!=0){
//            v = ((YYSR-YYCB)/YYSR);
//        }
//        if(StringUtils.isEmpty(YYSRS) && StringUtils.isEmpty(YYCBS)){
//            return null;
//        }
//        return getIaadIndicatorGradeDetail(indicator,v+"",0,String.format("近一年营业收入%s,近一年营业成本%s",YYSR,YYCB));
    }

//    净利润/净资产（三表）
    /**
     5、净利润/净资产 --》JLR/JZC
     --》M-6、LRM.JLR、ZCFZ.SYZQYHJ
     "1、从三表获取基础数据；
     2、净资产=所有者权益
     3、近一年及一期取计算结果较低的值"
     单位：%
     */
    private IaadIndicatorGradeDetail getJLR_JZC(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("JLR-JZC");
            ExtractBizModel modelZCFZ = getModel("M-6","ZCFZ");
            ExtractBizModel modelLR = getModel("M-6","LR");
            Map<String, ExtractItem> JZCcol1 = modelZCFZ.getTable(getJYN(modelZCFZ.getTables()));
            Map<String, ExtractItem> JZCcol2 = modelZCFZ.getTable(getJYQ(modelZCFZ.getTables()));
            Map<String, ExtractItem> JLRcol1 = modelLR.getTable(getJYN(modelLR.getTables()));
            Map<String, ExtractItem> JLRcol2 = modelLR.getTable(getJYQ(modelLR.getTables()));
            double JZCv1=0,JZCv2=0,JLRv1=0,JLRv2=0,v1=0,v2=0;
            String JZCSv1=null,JZCSv2=null,JLRSv1=null,JLRSv2=null;
            int pageNum = getChapterModel("M-6").getStartPage();
            if(MapUtils.isNotEmpty(JZCcol1) && JZCcol1.get("SYZQYHJ")!=null && JZCcol1.get("SYZQYHJ").getValue()!=null){
                JZCSv1=JZCcol1.get("SYZQYHJ").getValue().getText();
                JZCv1 = getDouble(JZCSv1);
            }
            if(MapUtils.isNotEmpty(JZCcol2) && JZCcol2.get("SYZQYHJ")!=null && JZCcol2.get("SYZQYHJ").getValue()!=null){
                JZCSv2=JZCcol2.get("SYZQYHJ").getValue().getText();
                JZCv2 = getDouble(JZCSv2);
            }
            if(MapUtils.isNotEmpty(JLRcol1) && JLRcol1.get("JLR")!=null && JLRcol1.get("JLR").getValue()!=null){
                JLRSv1=JLRcol1.get("JLR").getValue().getText();
                JLRv1 = getDouble(JLRSv1);
            }
            if(MapUtils.isNotEmpty(JLRcol2) && JLRcol2.get("JLR")!=null && JLRcol2.get("JLR").getValue()!=null){
                JLRSv2=JLRcol2.get("JLR").getValue().getText();
                JLRv2 = getDouble(JLRSv2);
            }
            if(JZCv1!=0 && JLRv1!=0){
                v1 = getTwoDecimal(JLRv1/JZCv1);
            }
            if(JZCv2!=0 && JLRv2!=0){
                v2 = getTwoDecimal(JLRv2/JZCv2);
            }
            if(StringUtils.isEmpty(JZCSv1) && StringUtils.isEmpty(JZCSv2) && StringUtils.isEmpty(JLRSv1) && StringUtils.isEmpty(JLRSv2)){
                return null;
            }
            JZCSv1 = JZCSv1 + modelZCFZ.getUnitType();
            JZCSv2 = JZCSv2 + modelZCFZ.getUnitType();
            JLRSv1 = JLRSv1 + modelLR.getUnitType();
            JLRSv2 = JLRSv2 + modelLR.getUnitType();
            return getIaadIndicatorGradeDetail(indicator,(v1>v2?doubleToBigDecimal(v2):doubleToBigDecimal(v1)),"%",pageNum,String.format("近一年净利润%s，近一年净资产%s，近一期净利润%s，近一期净资产%s",JLRSv1,JZCSv1,JLRSv2,JZCSv2));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

//    经营性现金流量净额
    /**
     6、经营性现金流量净额 --》XJLL
     --》M-6、XJLL.JYHDCSXJLLJE
     "1、三表获取；
     2、获取近一年及一期；"
     */
    private IaadIndicatorGradeDetail getXJLL(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("JYXXJLLJE");
            double v1=0,v2=0;
            String s1=null,s2=null;
            String thresholdValue=null,automateScore=null,thresholdScore=null;
            ExtractBizModel model = getModel("M-6","XJLL");
            Map<String, ExtractItem> col1 = model.getTable(getJYN(model.getTables()));
            Map<String, ExtractItem> col2 = model.getTable(getJYQ(model.getTables()));
            int pageNum = getChapterModel("M-6").getStartPage();
            if(col1!=null && col2!=null && indicator!=null){
                KVText value1 = col1.get("JYHDCSXJLLJE")!=null?col1.get("JYHDCSXJLLJE").getValue():null;
                KVText value2 = col2.get("JYHDCSXJLLJE")!=null?col2.get("JYHDCSXJLLJE").getValue():null;
                if(value1!=null && value2!=null){
                    pageNum = value1.getPageNumber();
                    s1 = value1.getText();
                    v1 = getDouble(s1);
                    s2 = value2.getText();
                    v2 = getDouble(s2);
                    Map map = getXJLLScoreAndThresholdValue(v1,v2,model.getUnitType(),indicator.getWeight(),indicator.getThresholdList());
                    thresholdValue = (String) map.get("thresholdValue");
                    automateScore = (String) map.get("automateScore");
                    thresholdScore = (String) map.get("thresholdScore");
                }
            }
            if(StringUtils.isEmpty(s1) && StringUtils.isEmpty(s2)){
                return null;
            }
            s1 = s1 + model.getUnitType();
            s2 = s2 + model.getUnitType();
            return getIaadIndicatorGradeDetail(indicator,(v1!=0 || v2!=0 ? doubleToBigDecimal(v1)+";"+doubleToBigDecimal(v2) : null),automateScore,thresholdValue,thresholdScore,pageNum,String.format("近一年经营性现金流量净额%s，近一期经营性现金流量净额%s",s1,s2));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

//    经营性现金流量净额分数及阈值计算
    private Map getXJLLScoreAndThresholdValue(double v1,double v2,String unit,String weight,List<IndustryIndicatorThreshold> industryIndicatorThresholdList){
        Map rs = new HashMap();
        double automateScore = 0;//得分 = 阈值对应分数 * 权重
        int score = 0,thresholdScore=0;
        String thresholdValue = null;
        v1 = getActualValue("亿元",unit,v1);
        v2 = getActualValue("亿元",unit,v2);
        if(v1>0 && v2>0 && v1>500){
            thresholdScore = score = 10;
            thresholdValue = "近一年一期为正，且近一年大于500亿元";
        }else if(v1>0 && v2>0 && v1>100){
            thresholdScore = score = 30;
            thresholdValue = "近一年一期为正，且近一年大于100亿元";
        }else if(v1>0 && v2>0 && v1<=100){
            thresholdScore = score = 50;
            thresholdValue = "近一年一期为正，但近一年小于100亿元";
        }else if(v1<0 || v2<0){
            thresholdScore = score = 80;
            thresholdValue = "近一年或近一期为负";
        }else if(v1<0 && v2<0){
            thresholdScore = score = 100;
            thresholdValue = "近一年一期均为负";
        }else{
            thresholdValue="未匹配到阈值";
        }
        automateScore = score*Double.valueOf(weight);
        rs.put("automateScore",automateScore+"");
        rs.put("thresholdValue",thresholdValue);
        rs.put("thresholdScore",thresholdScore==0?null:thresholdScore+"");
        return rs;
    }

//  受限资产/总资产
    /**
     7、受限资产/总资产 --》SXZC/ZZC
     --》M-6、SXZC.SXZC、ZCFZ.ZCZJ
     "1、从M-2、M-6章节获取受限资产，不存在提示缺失；
     2、总资产、受限资产近一期数据；
     3、受限资产不存在提示缺失；"
     单位：%
     */
    private IaadIndicatorGradeDetail getSXZC_ZZC(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("SXZC-ZZC");
            KVText SXZC_kvText = getKVTextValue("M-6","SXZC","SXZC");
            int pageNum = getChapterModel("M-6").getStartPage();
            double SXZC=0,ZCZJ=0,v=0;
            String SXZCS=null,ZCZJS=null;
            ExtractBizModel model = getModel("M-6","ZCFZ");
            if(SXZC_kvText!=null){
                Map<String, ExtractItem> col = model.getTable(getJYQ(model.getTables()));
                if(MapUtils.isNotEmpty(col) && col.get("ZCZJ")!=null && col.get("ZCZJ").getValue()!=null){
                    SXZCS = SXZC_kvText.getText();
                    SXZC = getDouble(SXZCS);
                    ZCZJS = col.get("ZCZJ").getValue().getText();
                    ZCZJ = getDouble(ZCZJS);
                    if(SXZC!=0 && ZCZJ!=0){
                        String SXZCU= getModel("M-6","SXZC").getUnitType();
                        String ZCZJU = col.get("ZCZJ").getUnit();
                        if(stringFilter(SXZCU).equals(stringFilter(ZCZJU))){
                            v = getTwoDecimal(SXZC/ZCZJ);
                        }else{
                            v = getTwoDecimal(SXZC/ZCZJ);
                        }
                    }
                }
            }
            if(StringUtils.isEmpty(SXZCS) && StringUtils.isEmpty(ZCZJS)){
                return null;
            }
            SXZCS = SXZC+getModel("M-6","SXZC").getUnitType();
            ZCZJS = ZCZJS+model.getUnitType();
            return getIaadIndicatorGradeDetail(indicator,doubleToBigDecimal(v),"%",pageNum,String.format("近一期受限资产%s，近一期总资产%s",SXZCS,ZCZJS));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

//  主体评级
    /**
     8、外部主体评级 --》ZTPJ
     --》M-0、FXR.XYPJ
     "1、从M-7获取评级、展望
     2、最新一期"
     */
    private IaadIndicatorGradeDetail getZTPJ(){
        try{
            IndustryIndicatorList indicator = getIndustryIndicatorList("WBZTPJ");
            // 评级从M0获取、M0如果不存在展望，则从M7获取
            KVText XYPJ = getKVTextValue("M-0","FXR","XYPJ");
            KVText ZW = getKVTextValue("M-0","FXR","ZW");
            if(ZW==null || (ZW!=null && StringUtils.isEmpty(ZW.getText()))){
                ZW = getKVTextValue("M-7","FXR","ZW");
            }
            int pageNum = getChapterModel("M-0").getStartPage();
            if(indicator!=null){
                String PJ = XYPJ!=null?XYPJ.getText():null;//评级
                String ZWS = ZW!=null?ZW.getText():null;//展望
                if(StringUtils.isEmpty(PJ) && StringUtils.isEmpty(ZWS)){
                    return null;
                }
                if(XYPJ!=null) pageNum = XYPJ.getPageNumber();
                Map map = getZTPJScoreAndThresholdValue(PJ,ZWS,indicator.getWeight(),indicator.getThresholdList());
                String thresholdValue = (String) map.get("thresholdValue");
                String automateScore = (String) map.get("automateScore");
                String thresholdScore = (String) map.get("thresholdScore");
                return getIaadIndicatorGradeDetail(indicator,PJ,automateScore,thresholdValue,thresholdScore,pageNum,String.format("信用评级%s，展望%s",PJ,ZWS));
            }
            return null;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return null;
        }
    }

    //    外部主体评级分数及阈值计算
//    AAA稳定10	AAA负面至AA+稳定30	AA+负面至AA稳定50    	AA负面80  	AA-及以下100
//    AAA级、AA级、A级、BBB级、BB级、B级、CCC级、CC级、C级、D级，每个等级可用“+”、“-”符号进行微调，表示略高或略低于本等级，但不包括AAA+
    private Map getZTPJScoreAndThresholdValue(String pj,String zw,String weight,List<IndustryIndicatorThreshold> industryIndicatorThresholdList){
        Map rs = new HashMap();
        double automateScore = 0;//得分 = 阈值对应分数 * 权重
        int score = 0;
        String thresholdValue = null;
        if(StringUtils.isEmpty(pj) && StringUtils.isEmpty(zw)){
            rs.put("automateScore","0");
            rs.put("thresholdValue","未匹配到阈值");
            return rs;
        }else if(StringUtils.isNotEmpty(pj) && StringUtils.isEmpty(zw)){
            if((pj.contains("AAA") || pj.contains("AAA+")) && (pj.contains("稳定"))){
                score = 10;
                thresholdValue = "AAA稳定";
            }else if((pj.contains("AAA") && (pj.contains("负面"))) || (pj.contains("AA+") && (pj.contains("稳定")))){
                score = 30;
                thresholdValue = "AAA负面至AA+稳定";
            }else if((pj.contains("AA+") && (pj.contains("负面"))) || (pj.contains("AA") && (pj.contains("稳定")))){
                score = 50;
                thresholdValue = "AA+负面至AA稳定";
            }else if(pj.contains("AA") && (pj.contains("负面"))){
                score = 80;
                thresholdValue = "AA负面";
            }else if(pj.contains("AA-")){
                score = 100;
                thresholdValue = "AA-及以下";
            }else{
//            score = 100;
//            thresholdValue = "AA-及以下";
                thresholdValue="未匹配到阈值";
            }
        }else if(StringUtils.isNotEmpty(pj) && StringUtils.isNotEmpty(zw)){
            if((pj.contains("AAA") || pj.contains("AAA+")) && (pj.contains("稳定") || zw.contains("稳定"))){
                score = 10;
                thresholdValue = "AAA稳定";
            }else if((pj.contains("AAA") && (pj.contains("负面") || zw.contains("负面"))) || (pj.contains("AA+") && (pj.contains("稳定") || zw.contains("稳定")))){
                score = 30;
                thresholdValue = "AAA负面至AA+稳定";
            }else if((pj.contains("AA+") && (pj.contains("负面") || zw.contains("负面"))) || (pj.contains("AA") && (pj.contains("稳定") || zw.contains("稳定")))){
                score = 50;
                thresholdValue = "AA+负面至AA稳定";
            }else if(pj.contains("AA") && (pj.contains("负面") || zw.contains("负面"))){
                score = 80;
                thresholdValue = "AA负面";
            }else if(pj.contains("AA-")){
                score = 100;
                thresholdValue = "AA-及以下";
            }else{
//            score = 100;
//            thresholdValue = "AA-及以下";
                thresholdValue="未匹配到阈值";
            }
        }else{
            thresholdValue="未匹配到阈值";
        }
        automateScore = score*Double.valueOf(weight);
        rs.put("automateScore",automateScore+"");
        rs.put("thresholdValue",thresholdValue);
        return rs;
    }








    @Override
    public List<ComponentResult> handlerM() {
        return null;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        return null;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        return null;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        return null;
    }
}
